package com.htthorizon.gateway.filter;

import com.htthorizon.common.core.constant.CacheConstants;
import com.htthorizon.common.core.constant.HttpStatus;
import com.htthorizon.common.core.utils.JwtUtils;
import com.htthorizon.common.core.utils.ServletUtils;
import com.htthorizon.common.core.utils.ip.IpUtils;
import com.htthorizon.common.redis.service.RedisService;
import com.htthorizon.system.api.domain.SysOperLog;
import com.htthorizon.system.api.exception.BlackException;
import com.htthorizon.system.api.exception.KeyNotException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Brilliant
 */
@Component
public class BlackFilter implements GlobalFilter, Ordered {


    @Autowired
    private RedisService redisService;

    @Value("${unique}")
    private boolean unique;




    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        ServerHttpRequest request = exchange.getRequest();
        ServerHttpRequest.Builder mutate = request.mutate();
        if (!unique){
            return chain.filter(exchange.mutate().request(mutate.build()).build());
        }
        String url1 = request.getURI().getPath();
        List<String> list1 = request.getHeaders().get("authorization");
        String userName = null;
        if (list1 != null && !(list1.isEmpty())) {
            String token = list1.get(0);
            userName = JwtUtils.getUserName(token);
        }
        String ipAddr = IpUtils.getIpAddr();
        String cacheObject = redisService.getCacheObject(CacheConstants.BLACK_IP);
        InetSocketAddress remoteAddress = request.getRemoteAddress();
        InetAddress address = remoteAddress.getAddress();
        String hostAddress = address.getHostAddress();
        if(Objects.isNull(cacheObject)){
            return chain.filter(exchange.mutate().request(mutate.build()).build());
        }
        String[] split = cacheObject.split(",");
        for (String ip:split){
            if (hostAddress.equals(ip)){
                SysOperLog sysLogininfor = SysOperLog.build(userName, IpUtils.getIpAddr1( exchange.getRequest()), "未在指定IP范围登录",request.getMethod().toString(),url1,request);
                redisService.setCacheObject("log:"+sysLogininfor.getCreateTime(),sysLogininfor);
                throw new BlackException();
            }
        }
        return chain.filter(exchange.mutate().request(mutate.build()).build());
    }

    @Override
    public int getOrder() {
        return 10000;
    }
}
